
# Load the necessary packages
library(tidyverse)
library(haven)
library(gsynth)
library(panelView)

# Set the working directory
path = "~/Dropbox/Mariel Effects/"

# Call in the data
data = read_dta(paste0(path, "Replication/data/county_analysis_data.dta")) %>%
	select(rank_log_pop_diff, year, state, county, treated, rep_vs) %>% ## filter(year<=1984) %>%
	mutate(txp = (treated==1)*(year>=1980))

# Run the analysis using 250 control counties
mc250 = gsynth(rep_vs ~ txp, 
	data = filter(data, rank_log_pop_diff<=253), 
	estimator = "mc", index = c("rank_log_pop_diff", "year"), 
	se = FALSE, r = c(0, 4), min.T0 = 5,
	CV = TRUE, force = "two-way", parallel = TRUE, 
	cores = 4, inference = "nonparametric")

# Run the analysis using 555 control counties
mc555 = gsynth(rep_vs ~ txp, 
	data = filter(data, rank_log_pop_diff<=555),
	estimator = "mc", index = c("rank_log_pop_diff", "year"), 
	se = FALSE, r = c(0, 4), min.T0 = 5,
	CV = TRUE, force = "two-way", parallel = TRUE, 
	cores = 4, inference = "nonparametric")

# Prepare and save the output
mc_output = tibble(year = mc250$time, control250 = mc250$Y.ct, 
	treated250 = mc250$Y.t, effect250 = mc250$att, 
	control555 = mc555$Y.ct, treated555 = mc555$Y.t, 
	effect555 = mc555$att)
write_csv(mc_output, paste0(path, "Replication/modified_data/matrix_completion_output.csv"))


